/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Desktop_DeleteAddress.java
 *
 * Created on Apr 7, 2011, 1:06:10 PM
 */
package DesktopGUIpackage;

import Controller.DesktopController;
import Controller.DesktopControllerHelper;
import DTO.Address;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author edaype
 */
public class Desktop_DeleteAddress extends javax.swing.JPanel {

    private DesktopController desktopController = null;
    private DesktopControllerHelper desktopControllerHelper = null;
    private List<Address> addList = null;
    private boolean success = false;

    /** Creates new form Desktop_DeleteAddress */
    public Desktop_DeleteAddress(DesktopControllerHelper desktopControllerHelper) {
        this.desktopControllerHelper = desktopControllerHelper;
        desktopController = desktopControllerHelper.getDesktopController();
        initComponents();
         confirmLabel1.setText("");
        customInit();
    }

    private void customInit() {

        if (success) {
            confirmLabel.setText(" address deleted");
        }
        confirmLabel.setText("");

        Integer custId = desktopController.getSessionMgr().getCutomerId();
        addList = desktopController.getCustomerAddresses(custId);
        final List<Address> finalList = addList;

        jList1.setModel(new javax.swing.AbstractListModel() {

            Object[] strings = finalList.toArray();

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        ButtonLayout = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        confirmLabel = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        confirmLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        jLabel8.setFont(new java.awt.Font("Tahoma", 2, 22));
        jLabel8.setText("Select your address");

        jList1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 16));
        jButton1.setText("Remove Selected");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        ButtonLayout.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 60));
        jLabel1.setText("Delete Address");

        javax.swing.GroupLayout ButtonLayoutLayout = new javax.swing.GroupLayout(ButtonLayout);
        ButtonLayout.setLayout(ButtonLayoutLayout);
        ButtonLayoutLayout.setHorizontalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 578, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(192, Short.MAX_VALUE))
        );
        ButtonLayoutLayout.setVerticalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        confirmLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        confirmLabel.setText("Confirmation Label");

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 16));
        jButton2.setText("Edit Selected");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        confirmLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        confirmLabel1.setText("errorLabel");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(confirmLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonLayout, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(240, 240, 240))
            .addGroup(layout.createSequentialGroup()
                .addGap(260, 260, 260)
                .addComponent(jButton1)
                .addContainerGap(617, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(260, 260, 260)
                .addComponent(jButton2)
                .addContainerGap(647, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(462, Short.MAX_VALUE)
                .addComponent(confirmLabel1)
                .addGap(507, 507, 507))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(confirmLabel1)
                .addGap(27, 27, 27)
                .addComponent(confirmLabel)
                .addGap(104, 104, 104)
                .addComponent(ButtonLayout, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jList1MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        if (!jList1.getValueIsAdjusting()) {

            if(jList1.getSelectedIndex() != -1){

                    Integer aid = addList.get(jList1.getSelectedIndex()).getID();
                    Integer cid = desktopController.getSessionMgr().getCutomerId();
                    success = desktopController.deleteCustomerAddress(cid, aid);
            }else{
                confirmLabel1.setText("please slect an address from list to delete");
            }

        }
        customInit();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        Address addressVO = new Address();
        if(jList1.getSelectedIndex() != -1){
            addressVO.setID(addList.get(jList1.getSelectedIndex()).getID());
            addressVO.setAptNo(addList.get(jList1.getSelectedIndex()).getAptNo());
            addressVO.setCity(addList.get(jList1.getSelectedIndex()).getCity());
            addressVO.setPostCode(addList.get(jList1.getSelectedIndex()).getPostCode());
            addressVO.setProvince(addList.get(jList1.getSelectedIndex()).getProvince());
            addressVO.setStNo(addList.get(jList1.getSelectedIndex()).getStNo());
            addressVO.setStreet(addList.get(jList1.getSelectedIndex()).getStreet());

        desktopControllerHelper.editAddressScreen(addressVO);
        }else{
                confirmLabel1.setText("please slect an address from list to edit ");
            }
       customInit();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ButtonLayout;
    private javax.swing.JLabel confirmLabel;
    private javax.swing.JLabel confirmLabel1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
